package com.dangz.dans.utils;

import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;

import java.util.List;

/**
 * PageUtil : 公共分页 同时 支持mybaits 和 jpa
 */

public class PageUtil<T> {

    //当前页
    private int curPage;
    //请求时每页的数量
    private int pageSize;

    //当前页的总记录数
    private int curPageSize;

    //总记录数
    private long totalRecords;
    //总页数
    private int totalPages;
    //结果集
    private List<T> content;

    //是否为第一页
    private boolean isFirstPage = false;
    //是否为最后一页
    private boolean isLastPage = false;
    //是否有上一页
    private boolean hasPrePage;
    //是否有下一页
    private boolean hasNextPage;



    public PageUtil() {
        super();
    }


    public PageUtil(PageInfo<T> pageInfo){
        super();
        this.curPage = pageInfo.getPageNum();
        this.pageSize = pageInfo.getPageSize();
        this.curPageSize = pageInfo.getSize();
        this.totalRecords = pageInfo.getTotal();
        this.totalPages = pageInfo.getPages();
        this.content = pageInfo.getList();
        this.isFirstPage = pageInfo.isIsFirstPage();
        this.isLastPage = pageInfo.isIsLastPage();
        this.hasNextPage = pageInfo.isHasNextPage();
        this.hasPrePage = pageInfo.isHasPreviousPage();
    }


    /**
     * Jpa方式
     * @param page
     */
    public PageUtil(Page<T> page){
        super();
        this.curPage = page.getNumber()+1;
        this.pageSize = page.getSize();
        this.curPageSize = page.getNumberOfElements();
        this.totalPages = page.getTotalPages();
        this.totalRecords = page.getTotalElements();
        this.content = page.getContent();
        this.isFirstPage = page.isFirst();
        this.isLastPage = page.isLast();
        this.hasNextPage = page.hasNext();
        this.hasPrePage = page.hasPrevious();
    }



    public int getCurPage() {
        return curPage;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public long getTotalRecords() {
        return totalRecords;
    }

    public void setTotalRecords(long totalRecords) {
        this.totalRecords = totalRecords;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }

    public List<T> getContent() {
        return content;
    }

    public void setContent(List<T> content) {
        this.content = content;
    }

    public boolean isFirstPage() {
        return isFirstPage;
    }

    public void setFirstPage(boolean firstPage) {
        isFirstPage = firstPage;
    }

    public boolean isLastPage() {
        return isLastPage;
    }

    public void setLastPage(boolean lastPage) {
        isLastPage = lastPage;
    }


    public int getCurPageSize() {
        return curPageSize;
    }

    public void setCurPageSize(int curPageSize) {
        this.curPageSize = curPageSize;
    }

    public boolean isHasPrePage() {
        return hasPrePage;
    }

    public void setHasPrePage(boolean hasPrePage) {
        this.hasPrePage = hasPrePage;
    }

    public boolean isHasNextPage() {
        return hasNextPage;
    }

    public void setHasNextPage(boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }
}


